package com.fwtai.service.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.tool.ToolClient;
import com.fwtai.tool.ToolString;
import com.fwtai.web.CouponDao;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 商品优惠券业务层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @Email service@fwtai.com
 * @创建日期 2024-01-04 18:08:42
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class CouponService{

  private final CouponDao couponDao;

  private final ThreadPoolTaskExecutor taskExecutor;

  public CouponService(final CouponDao couponDao,final ThreadPoolTaskExecutor taskExecutor){
    this.couponDao = couponDao;
    this.taskExecutor = taskExecutor;
  }

  public String add(final HttpServletRequest request){
    return CompletableFuture.supplyAsync(()->{
      final PageFormData formData = new PageFormData(request);
      final String p_goods_id = "goods_id";
      final String p_values = "values";
      final String validate = ToolClient.validateField(formData,p_goods_id,p_values);
      if(validate != null) return validate;
      final String values = formData.getString(p_values);
      final ArrayList<HashMap<String,Object>> list = ToolString.parseArrayListHashMap(values);
      if(list.isEmpty()){
        return ToolClient.jsonValidateField();
      }
      final Long goods_id = formData.getLong(p_goods_id);
      for(int i = list.size() - 1; i >= 0; i--){
        final HashMap<String,Object> map = list.get(i);
        final String goodsId = (String)map.get("goods_id");
        final String number = (String)map.get("quantity");
        map.put("type",Integer.parseInt((String)map.get("type")));
        if(goodsId != null){//编辑时
          if(number == null){
            map.remove("quantity");
          }else{
            map.put("quantity",Integer.parseInt(number));
          }
          map.put("goods_id",Long.parseLong((String) map.get("goods_id")));
          map.put("area_id",Long.parseLong((String) map.get("area_id")));
        }else{
          if(number == null){
            return ToolClient.createJsonFail("请输入优惠券数量");
          }
          map.put("quantity",Integer.parseInt(number));
          map.put("goods_id",goods_id);
          map.put("area_id",Long.parseLong((String) map.get("area_id")));
        }
      }
      return ToolClient.executeRows(couponDao.add(list));
    },taskExecutor).join();
  }

  public String edit(final HttpServletRequest request){
    return CompletableFuture.supplyAsync(()->{
      final PageFormData formData = new PageFormData(request);
      final String p_goods_id = "goods_id";
      final String p_values = "values";
      final String validate = ToolClient.validateField(formData,p_goods_id,p_values);
      if(validate != null) return validate;
      final String values = formData.getString(p_values);
      final ArrayList<HashMap<String,Object>> list = ToolString.parseArrayListHashMap(values);
      if(list.isEmpty()){
        return ToolClient.jsonValidateField();
      }
      for(int i = list.size() - 1; i >= 0; i--){
        final HashMap<String,Object> map = list.get(i);
        final String goodsId = (String)map.get("goods_id");
        final String number = (String)map.get("quantity");
        if(number == null){
          map.remove("quantity");
        }else{
          map.put("quantity",Integer.parseInt(number));
        }
        map.put("type",Integer.parseInt((String)map.get("type")));
        map.put("goods_id",Long.parseLong(goodsId));
        map.put("area_id",Long.parseLong((String) map.get("area_id")));
      }
      return ToolClient.executeRows(couponDao.add(list));
    },taskExecutor).join();
  }

  public String queryById(final Long kid){
    return CompletableFuture.supplyAsync(()->{
      if(kid == null){
        return ToolClient.jsonValidateField();
      }
      final Integer coupon = couponDao.getGoodsCoupon(kid);
      final List<HashMap<String,Object>> list = couponDao.queryById(kid);
      final HashMap<String,Object> result = new HashMap<>(2);
      result.put("coupon",coupon);
      if(!list.isEmpty()){
        result.put("list",list);
      }
      return ToolClient.queryJson(result);
    },taskExecutor).join();
  }

  public String getCouponTree(final String kid,final String goodsId){
    return CompletableFuture.supplyAsync(()->{
      final HashMap<String,Long> map = new HashMap<>(2);
      if(goodsId == null || goodsId.length() == 0){
        return ToolClient.jsonValidateField();
      }
      map.put("goodsId",Long.parseLong(goodsId));
      if(kid != null && kid.length() > 0){
        map.put("pid",Long.parseLong(kid));
        return ToolClient.queryJson(couponDao.getGoodsArea(map));
      }else{
        map.put("pid",0L);
        return ToolClient.queryJson(couponDao.getGoodsArea(map));
      }
    },taskExecutor).join();
  }

  public String listData(final PageFormData formData){
    return CompletableFuture.supplyAsync(()->{
      try{
        if(ToolClient.dataTableMysql(formData) == null) return ToolClient.jsonValidateField();
        final CompletableFuture<List<HashMap<String,Object>>> listData = CompletableFuture.supplyAsync(() -> couponDao.getListData(formData),taskExecutor);
        final CompletableFuture<Integer> listTotal = CompletableFuture.supplyAsync(() -> couponDao.getListTotal(formData),taskExecutor);
        return ToolClient.dataTableOK(listData.join(),listTotal.join());
      } catch (final Exception ignored){
        return ToolClient.dataTableException();
      }
    },taskExecutor).join();
  }

  public String delCoupon(final String goodsId,final String areaId){
    return CompletableFuture.supplyAsync(()->{
      if(goodsId == null || goodsId.length() == 0 || areaId == null || areaId.length() == 0){
        return ToolClient.jsonValidateField();
      }
      try {
        final HashMap<String,Object> map = new HashMap<>(2);
        map.put("goods_id",Long.parseLong(goodsId));
        map.put("area_id",Long.parseLong(areaId));
        return ToolClient.executeRows(couponDao.delCoupon(map));
      } catch (final Exception ignored) {
        return ToolClient.jsonValidateField("参数类型有误");
      }
    },taskExecutor).join();
  }
}